package com.zy.system.service.impl;

import com.zy.model.system.SysUser;
import com.zy.system.custom.CustomUser;
import com.zy.system.service.SysMenuService;
import com.zy.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @Author Gong
 * @Description
 * @dATE 2023/6/12 10:12
 **/
@Component
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getUserInfoByUserName(username);
        if(sysUser  ==null){
            throw  new UsernameNotFoundException("用户不存在");
        }

        if(sysUser.getStatus().intValue()  == 0){
            throw new RuntimeException("用户被禁用了");
        }
        //这里的查询是具体的权限标记
        List<String> userPermsList = sysMenuService.getUserButtonList(sysUser.getId());
        //转换security要求格式数据
        List<SimpleGrantedAuthority> authorities = new ArrayList<>( );
        for ( String perm: userPermsList) {
            authorities.add(new SimpleGrantedAuthority(perm.trim( )));
        }
        return new CustomUser(sysUser, authorities);
    }
}
